<?php

namespace App\Http\Requests\Api;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class SendMessageRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title' => 'required|max:180',
            'content' => 'required|max:240',
            'toId' => 'required'
        ];
    }


    public function messages()
    {
        return [
            'title.required' => '请输入标题',
            'title.max' => '标题长度不能超过180个字符',
            'content.required' => '请输入内容',
            'content.max' => '内容长度不能超过240个字符',
            'toId.required' => '请选择发送人'
        ];
    }

    protected function failedValidation(Validator $validator) {
        $error= $validator->errors()->all();
        throw new HttpResponseException(response()->json(['message'=>$error[0],'code'=>'400'], 400));
    }
}
